package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetMealDishService;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
@Api(tags = "C端-套餐浏览接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SetMealDishService setMealDishService;

    @Autowired
    private DishService dishService;
    @GetMapping("/list")
    @ApiOperation("查询套餐集合")
    @Cacheable(cacheNames = "setmealCatch",key = "#categoryId")
    public Result<List<Setmeal>> list(Long categoryId){
        Setmeal setmeal = new Setmeal();

        List<Setmeal> list = setMealService.lambdaQuery()
//                .like(Setmeal::getName,setmeal.getName())
                .eq( Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, StatusConstant.ENABLE)
                .list();


        return Result.success(list);

    }

    @GetMapping("/dish/{id}")
    @ApiOperation("跟据套餐id查询菜品")
    @Cacheable(cacheNames = "setmealCatchDish",key = "#id")
    public Result<List<DishItemVO>> dishId(@PathVariable("id") Long id){
//        对应套餐的名称份数
        List<SetmealDish> setmealDishes = setMealDishService.lambdaQuery()
                .eq(SetmealDish::getSetmealId, id)
                .list();

        List<DishItemVO> collect = setmealDishes.stream().map((item) -> {
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(item, dishItemVO);
            List<Dish> dishList = dishService.lambdaQuery()
                    .eq(Dish::getId, item.getDishId())
                    .list();
            dishList.stream().map((i) -> {
                BeanUtils.copyProperties(i, dishItemVO);
                return i;
            }).collect(Collectors.toList());
            return dishItemVO;
        }).collect(Collectors.toList());


        return Result.success(collect);
    }

}
